package com.gec;

import com.gec.entity.SysUser;
import com.gec.mapper.SysUserMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootApplication
@MapperScan(value = "com.gec.mapper")
public class UserRoleApplication {

    @Autowired
    private SysUserMapper userMapper;

    public static void main(String[] args) {
        SpringApplication.run(UserRoleApplication.class, args);
    }

    //@PostConstruct
    public void JdbcIni() {
        //初始化数据库表内容
        Date curDate = new Date();

        List<GrantedAuthority> authorities = new ArrayList<>();
        GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_" + "USER");
        GrantedAuthority authority2 = new SimpleGrantedAuthority("ROLE_" + "ADMIN");
        authorities.add(authority);
        authorities.add(authority2);

        PasswordEncoder encoder = new BCryptPasswordEncoder();

        SysUser user = new SysUser(null,"admin",
                encoder.encode("admin"),
                true, true,
                true, true,
                "管理员",
                curDate, curDate, authorities);

        userMapper.insertSysUser(user);
    }
}
